﻿using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace Assist
{
	public class LineAssistFunc
	{
		public static XYZ GetVector(Line line)
		{
			return line.GetEndPoint(1) - line.GetEndPoint(0);
		}

		public static OverlapType GetOverlapType(UIApplication uiApp, Line line1, Line line2)
		{
			XYZ vector = LineAssistFunc.GetVector(line1);
			XYZ vector2 = LineAssistFunc.GetVector(line2);
			bool flag = !GeoUtil.IsParallel(vector, vector2);
			OverlapType result;
			if (flag)
			{
				result = OverlapType.eNotOverlap;
			}
			else
			{
				XYZ hyendPoint = line1.GetEndPoint(0);
				XYZ closestPointTo = CurveAssistFunc.GetClosestPointTo(uiApp, line2, hyendPoint, false);
				bool flag2 = closestPointTo == null;
				bool flag3 = !flag2 && closestPointTo.DistanceTo(line1.GetEndPoint(0)) < LineAssistFunc.precision;
				hyendPoint = line1.GetEndPoint(1);
				closestPointTo = CurveAssistFunc.GetClosestPointTo(uiApp, line2, hyendPoint, false);
				bool flag4 = closestPointTo == null;
				bool flag5 = !flag4 && closestPointTo.DistanceTo(line1.GetEndPoint(1)) < LineAssistFunc.precision;
				hyendPoint = line2.GetEndPoint(0);
				closestPointTo = CurveAssistFunc.GetClosestPointTo(uiApp, line1, hyendPoint, false);
				bool flag6 = closestPointTo == null;
				bool flag7 = !flag6 && closestPointTo.DistanceTo(line2.GetEndPoint(0)) < LineAssistFunc.precision;
				hyendPoint = line2.GetEndPoint(1);
				closestPointTo = CurveAssistFunc.GetClosestPointTo(uiApp, line1, hyendPoint, false);
				bool flag8 = closestPointTo == null;
				bool flag9 = !flag8 && closestPointTo.DistanceTo(line2.GetEndPoint(1)) < LineAssistFunc.precision;
				OverlapType overlapType = OverlapType.eNotOverlap;
				bool flag10 = flag3 && flag5 && flag7 && flag9;
				if (flag10)
				{
					overlapType = OverlapType.eOverlap;
				}
				else
				{
					bool flag11 = (flag3 && flag5 && flag7 && !flag9) || (flag3 && flag5 && !flag7 && flag9) || (flag3 && flag5 && !flag7 && !flag9);
					if (flag11)
					{
						overlapType = OverlapType.e2Cover1;
					}
					else
					{
						bool flag12 = (!flag3 && flag5 && flag7 && flag9) || (flag3 && !flag5 && flag7 && flag9) || (!flag3 && !flag5 && flag7 && flag9);
						if (flag12)
						{
							overlapType = OverlapType.e1Cover2;
						}
						else
						{
							bool flag13 = (flag3 && !flag5 && flag7 && !flag9) || (flag3 && !flag5 && !flag7 && flag9) || (!flag3 && flag5 && flag7 && !flag9) || (!flag3 && flag5 && !flag7 && flag9);
							if (flag13)
							{
								overlapType = OverlapType.ePartOverlap;
							}
						}
					}
				}
				result = overlapType;
			}
			return result;
		}

		public static bool ResetLine(Line lineCurve, XYZ ptStart, XYZ ptEnd)
		{
			XYZ hyendPoint = lineCurve.GetEndPoint(0);
			XYZ hyendPoint2 = lineCurve.GetEndPoint(1);
			double num = lineCurve.GetYJKEndParameter(0);
			double num2 = lineCurve.GetYJKEndParameter(1);
			bool flag = ptStart.DistanceTo(hyendPoint) < LineAssistFunc.precision;
			if (flag)
			{
				double num3 = ptEnd.DistanceTo(hyendPoint2);
				num2 -= num3;
			}
			else
			{
				bool flag2 = ptEnd.DistanceTo(hyendPoint) < LineAssistFunc.precision;
				if (flag2)
				{
					double num4 = ptStart.DistanceTo(hyendPoint2);
					num2 -= num4;
				}
				else
				{
					bool flag3 = ptStart.DistanceTo(hyendPoint2) < LineAssistFunc.precision;
					if (flag3)
					{
						double num5 = ptEnd.DistanceTo(hyendPoint);
						num += num5;
					}
					else
					{
						bool flag4 = ptEnd.DistanceTo(hyendPoint2) < LineAssistFunc.precision;
						if (flag4)
						{
							double num6 = ptStart.DistanceTo(hyendPoint);
							num += num6;
						}
					}
				}
			}
			lineCurve.MakeBound(num, num2);
			return true;
		}

		public static double AngleToXAxis(Line line)
		{
			XYZ vector = LineAssistFunc.GetVector(line);
			return vector.AngleOnPlaneTo(XYZ.BasisX, XYZ.BasisZ);
		}

		public static XYZ Midpoint(Line line)
		{
			return new XYZ((line.GetEndPoint(0).X + line.GetEndPoint(1).X) / 2.0, (line.GetEndPoint(0).Y + line.GetEndPoint(1).Y) / 2.0, (line.GetEndPoint(0).Z + line.GetEndPoint(1).Z) / 2.0);
		}

		protected static double precision = 1E-06;
	}
}
